package com.art.sunflower.controller;

import com.art.sunflower.exception.NullMasterException;
import com.art.sunflower.service.MasterService;
import com.art.sunflower.util.Message;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.security.NoSuchAlgorithmException;

@RestController
@CrossOrigin
@RequestMapping(value = "/master")
@Api(value = "课程管理员控制器")
public class MasterController {

    @Resource
    MasterService masterService;

    @RequiresRoles(value = {"admin"})
    @ApiOperation(value = "分页获取课程管理员数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "页大小", dataType = "int", paramType = "query", defaultValue = "5"),
            @ApiImplicitParam(name = "pageNo", value = "页码", dataType = "int", paramType = "query", defaultValue = "1"),})
    @GetMapping()
    public Message getList(Integer pageSize, Integer pageNo) {
        try {
            return Message.createSuc(masterService.getList(pageSize, pageNo));
        } catch (NullMasterException e) {
            e.printStackTrace();
            return Message.createErr("课程管理员不存在");
        }
    }

    @RequiresRoles(value = {"admin"})
    @ApiOperation(value = "增加一条课程管理员数据")
    @PostMapping()
    public Message add(String username, String password, String phone, String name) {
        try {
            masterService.add(username, password, phone, name);
            return Message.createSuc("添加成功");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return Message.createErr("添加失败");
        }
    }

    @RequiresRoles(value = {"admin"})
    @ApiOperation(value = "修改一条课程管理员数据")
    @PutMapping()
    public Message update(Integer id, String username, String password, String phone, String realName) {
        String msg = "修改成功";
        try {
            masterService.update(id, username, password, phone, realName);
            return Message.createSuc(msg);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            msg = "修改失败";
        } catch (NullMasterException e) {
            e.printStackTrace();
            msg = "课程管理员不存在";
        }
        return Message.createErr(msg);
    }

    @RequiresRoles(value = {"admin"})
    @ApiOperation(value = "删除一条课程管理员数据")
    @DeleteMapping(value = "/{id}")
    public Message delete(@PathVariable Integer id) {
        String msg = "删除成功";
        try {
            masterService.delete(id);
            return Message.createSuc(msg);
        } catch (NullMasterException e) {
            e.printStackTrace();
            msg = "课程管理员不存在";
        }
        return Message.createErr(msg);
    }

    @RequiresRoles(value = {"admin"})
    @ApiOperation(value = "查看一条课程管理员数据")
    @GetMapping(value = "/{id}")
    public Message get(@PathVariable Integer id) {
        String msg;
        try {
            return Message.createSuc(masterService.getById(id));
        } catch (NullMasterException e) {
            e.printStackTrace();
            msg = "课程管理员不存在";
        }
        return Message.createErr(msg);
    }

}
